package zth.com.kotlindemo.adapter

import SunStarUtils.GlideUtil
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ImageView
import android.widget.TextView
import zth.com.kotlindemo.R
import zth.com.kotlindemo.R.id.main_menu_img
import zth.com.kotlindemo.data.MenuData

/**
 * Created by Sun.Star on 2017/5/18.
 */
class MainMenuAdapter(var context : Context ,var list: List<MenuData>) : RecyclerView.Adapter<MainMenuAdapter.MainMenuHolder>() {

    var itemClickListener: AdapterView.OnItemClickListener? = null
    //创建view holder
    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MainMenuHolder
            = MainMenuHolder(LayoutInflater.from(context).inflate(R.layout.main_menu_item,parent,false))


    override fun onBindViewHolder(holder: MainMenuHolder?, position: Int) {
        //显示图片
        holder!!.iv!!.setImageResource(list.get(position).img)
        //显示名称
        holder!!.tv!!.text = list.get(position).name
        //设置点击事件
        holder.itemView.setOnClickListener {
            if(itemClickListener != null){
                itemClickListener!!.onItemClick(null,holder.itemView,position,holder.itemId)
            }
        }
    }

    override fun getItemCount(): Int = list.size


    class MainMenuHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
        var tv: TextView?=null
        var iv: ImageView?=null
        init {
            tv = itemView!!.findViewById(R.id.main_menu_text) as TextView
            iv = itemView!!.findViewById(R.id.main_menu_img) as ImageView
        }
    }
}